package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 员工管理
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

       // Employee emp = employeeService.getOne(Wrappers.lambdaQuery(Employee.class).eq(Employee::getUsername, employee.getUsername()));

        //判断用户名
        if (emp == null) {
            return R.error("用户名错误");
        }

        //密码加密
        String password1 = employee.getPassword();
        String password2 = emp.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password1.getBytes());

        //判断密码
        if (!StringUtils.equals(password2, md5Password)) {
            return R.error("密码错误");
        }

        //判断员工状态
        if (emp.getStatus()==0){
            return R.error("员工已禁用");
        }

        //发送session
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);
    }

    /**
     *员工退出
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功");
    }

    /**
     * 新增员工
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {

        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 员工信息分页查询
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {

        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     *禁用,修改员工
     */
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee){

        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     *回显数据
     */
    @GetMapping("{id}")
    public R<Employee> selectById(@PathVariable Long id) {

        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}
